//
//  XMBaseModel.m
//  XMKit
//
//  Created by chenming on 2018/5/26.
//  Copyright © 2018年 ChenXiaoMing. All rights reserved.
//

#import "XMBaseModel.h"

@implementation XMBaseModel

- (instancetype)initWithXMDataModelDict:(NSDictionary *)dict {
    if (!dict || ![dict isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+ (instancetype)XMDataWithModeldict:(NSDictionary *)dict {
    return [[[self class] alloc] initWithXMDataModelDict:dict];
}

- (void)setValue:(id)value forKey:(NSString *)key {
    
    if ([value isKindOfClass:[NSNull class]]) {
        
        return;
    }
    [super setValue:value forKey:key];
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}

/*
- (instancetype)initWithXMDataModelDict:(NSDictionary *)dict {
    if (!dict || ![dict isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    
    if (self = [super init]) {
        for (NSString *key in [dict allKeys]) {
            id value = dict[key];
            if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {
                //1.处理对象类型和数组类型
                [self setValue:value forKeyPath:key];
            } else if ([value isKindOfClass:[NSNull class]]) {
                [self setValue:nil forKey:key];
                //2.处理空类型:防止出现unRecognized selector exception
            } else {
                //处理关键字，将关键字转换一下，例如：id
                NSString *rKey = @"";
                if([key isEqualToString:@"id"]){
                    rKey = @"ID";
                }
                if([key isEqualToString:@"description"]){
                    rKey = @"Description";
                }
                //3.处理其他类型：包括数字，字符串，布尔，全部使用NSString来处理
                [self setValue:[NSString stringWithFormat:@"%@",value] forKeyPath:rKey]; //key
            }
            
        }
        // 直接赋值的处理
        //        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+ (instancetype)XMDataWithModeldict:(NSDictionary *)dict {
    return [[[self class] alloc] initWithXMDataModelDict:dict];
}

- (void)setValue:(id)value forKey:(NSString *)key {
    
    if ([value isKindOfClass:[NSNull class]]) {
        
        return;
    }
    [super setValue:value forKey:key];
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
}

- (void)setNilValueForKey:(NSString *)key {
    
}
*/

// 当model中与关键字冲突,使用这个方法替换
// - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
//     if([key isEqualToString:@"id"]) {
//        [self setValue:value forKey:self.ID];
//        self.ID = value;
//        return;
//        }
// }

@end
